package com.yuneec.fly.fragments;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Toast;

import com.yuneec.droneservice.parse.Wifi;
import com.yuneec.fly.dialogs.BaseDialogFragment;
import com.yuneec.fly.dialogs.LoginWifiDialog;
import com.yuneec.fly.events.EventBusFactory;
import com.yuneec.fly.events.uiEvent.BindEvent;
import com.yuneec.fly.events.uiEvent.SetWifiConfigurationEvent;
import com.yuneec.fly.events.uiEvent.ShowCameraIp;
import com.yuneec.fly.presenters.AbstractBindPresenter;
import com.yuneec.fly.presenters.BindCameraPresenter;
import com.yuneec.fly.utils.DialogHelper;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * Created by zd.zhang on 2016/7/15.
 */
public class BindCameraFragment extends BindModelFragment {

    @Override
    public AbstractBindPresenter newInstancePresemter() {
        return new BindCameraPresenter(getActivity());
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void setWifiConfiguration(final SetWifiConfigurationEvent event) {
        progressBar.setVisibility(View.GONE);
        if (event.tag != SetWifiConfigurationEvent.SET_PASSWORD) {
            return;
        }
        if (event.obj == null || !(event.obj instanceof String)) {
            return;
        }
        final String wifi = (String) event.obj;
        if (wifi == null) {
            return;
        }
        DialogHelper.showLoginWifiDialog(getFragmentManager(), wifi, new LoginWifiDialog.OnConfirmListener() {
            @Override
            public void onConfirm(BaseDialogFragment dialogFragment, String password) {
                InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, InputMethodManager.HIDE_NOT_ALWAYS);
                dialogFragment.dismiss();
                event.tag = SetWifiConfigurationEvent.ADD_CONFIGURATION;
                event.obj = password;
                EventBusFactory.getUiBus().post(event);
                progressBar.setVisibility(View.VISIBLE);
            }
        }, new BaseDialogFragment.OnCancelListener() {
            @Override
            public void onCancel(BaseDialogFragment dialogFragment) {
                presenter.resetState();
                InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, InputMethodManager.HIDE_NOT_ALWAYS);
            }
        });
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void showCameraIp(ShowCameraIp pi) {
        Activity a = getActivity();
        if (isAdded() && a != null) {
            Toast.makeText(a, "Camera Ip:" + pi.ip, Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onBind(BindEvent event) {
        super.onBind(event);
    }
}
